package com.loser.cmd;

import com.loser.module.base.common.utils.BroadCastUtils;
import com.loser.module.base.proto.broadcast.user.UserLoginOutBroadCast;
import com.loser.module.base.proto.sdk.user.UserLoginCmd;
import com.loser.module.base.proto.sdk.user.request.UserLogOutReq;
import com.loser.module.base.proto.sdk.user.request.UserLoginReq;
import com.loser.module.base.proto.sdk.user.resp.UserLogOutResult;
import com.loser.module.base.proto.sdk.user.resp.UserLoginResult;
import com.loser.user.mongo.handler.UserHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class UserLoginCmdHandler implements UserLoginCmd {

    @Autowired
    private UserHandler userHandler;

    @Override
    public UserLoginResult loginCmd(UserLoginReq req) {
        return userHandler.login(req);
    }

    @Override
    public UserLogOutResult logoutCmd(UserLogOutReq req) {

        UserLogOutResult result = new UserLogOutResult();
        result.setUserId(req.getUserId());
        result.setStatus(true);

        UserLoginOutBroadCast broadCast = new UserLoginOutBroadCast();
        broadCast.setUserId(req.getUserId());
        broadCast.setLogoutTime(System.currentTimeMillis());
        BroadCastUtils.sendBroadCast2All(broadCast);
        return result;

    }

}
